import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'big-user',
  () => {
    const token = ref(null)
    const time = ref(null)
    const setToken = (newToken) => {
      token.value = newToken
      time.value = Date.now() + 7200000;
    }
    const checkToken = () => {
      if(time.value < Date.now()){
        logout()
        removeToken()
        return false;
      }
      return true
    }
    const removeToken = () => {
      time.value = ''
      token.value = ''
    }

    const user = ref({})
    const is_login = ref(false)
    const login_success = (user_info_list) => {
      is_login.value = true;
      user.value = {}
      user.value = user_info_list
    }
    const logout = () => {
      user.value = ''
      is_login.value = false
    }

    const flag = ref('login')
    const setFlag = (newFlag) => {
      flag.value = newFlag
    }
    return {
      token,
      setToken,
      removeToken,
      user,
      is_login,
      login_success,
      logout,
      flag,
      setFlag,
      time,
      checkToken,
    }
  },
  {
    persist: true
  }
)

